package org.voyager.service.admin.scenic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.voyager.common.beans.JsonResult;
import org.voyager.common.utils.UUIDUtil;
import org.voyager.service.admin.scenic.dao.ScenicGroupAndSpotMapper;
import org.voyager.service.admin.scenic.dao.ScenicGroupMapper;
import org.voyager.service.admin.scenic.po.ScenicGroup;
import org.voyager.service.admin.scenic.po.ScenicGroupWithSpot;
import org.voyager.service.admin.scenic.po.ScenicSpotGroup;
import org.voyager.service.admin.scenic.service.ScenicGroupService;
import org.voyager.service.admin.scenic.vo.ScenicGroupSpotVo;


@Service
public class ScenicGroupServiceImpl implements ScenicGroupService {

	@Autowired
	private ScenicGroupMapper scenicGroupMapper;
	@Autowired
	private ScenicGroupAndSpotMapper scenicGroupAndSpotMapper;
	
	@Override
	@Transactional
	public JsonResult addGroup(ScenicGroup scenicGroup) {
		scenicGroup.setId(UUIDUtil.randomUUID32());
		scenicGroup.setCreateTime(new Date());
		scenicGroup.setUpdateTime(new Date());
		scenicGroup.setStatus((scenicGroup.getStatus()==null ? 1 : scenicGroup.getStatus()));
		if (scenicGroupMapper.insert(scenicGroup) == 1) {
			return JsonResult.ok();
		} else {
			return JsonResult.error("添加失败");
		}
	}

	@Override
	@Transactional
	public JsonResult delGroupById(String id) {
		if (scenicGroupMapper.deleteByPrimaryKey(id) == 1) {
			return JsonResult.ok();
		} else {
			return JsonResult.error("删除失败");
		}
	}

	@Override
	@Transactional
	public JsonResult modifyGroup(ScenicGroup scenicGroup) {
		scenicGroup.setUpdateTime(new Date());
		if (scenicGroupMapper.updateByPrimaryKeySelective(scenicGroup) == 1) {
			return JsonResult.ok();
		} else {
			return JsonResult.error("修改失败");
		}
	}

	@Override
	public JsonResult queryPageList(int pageNo, int pageSize, ScenicGroup scenicGroup) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JsonResult queryGroupAndSpotList(ScenicGroupWithSpot scenicGroupWithSpot) {
//		Map<String, Object> map = new HashMap<String, Object>();
		List<ScenicGroupWithSpot> list = scenicGroupAndSpotMapper.selectGroupAndSpotsByCondition(scenicGroupWithSpot);
		/*if (list != null) {
			List<ScenicGroupWithSpotVo> resultList= list.stream()
			.map(sgws -> {
				ScenicGroupWithSpotVo scenicGroupWithSpotVo = new ScenicGroupWithSpotVo();
				ScenicSpotVo scenicSpotVo = new ScenicSpotVo();
				BeanUtils.copyProperties(sgws, scenicSpotVo);
				if (sgws.getScenicSpots()!=null && sgws.getScenicSpots().size()>0) {
					scenicGroupWithSpotVo.setHaveScenicSpot("1");
				} else {
					scenicGroupWithSpotVo.setHaveScenicSpot("0");
				}
				return scenicGroupWithSpotVo;
			})
			.collect(Collectors.toList());
			map.put("scenicGroups", resultList);
		}*/
//		map.put("scenicGroups", list);
		return JsonResult.ok().putData("scenicGroups", list);
	}

	@Override
	@Transactional
	public JsonResult addSpot(ScenicGroupSpotVo scenicGroupSpotVo) {
		if (scenicGroupSpotVo != null) {
			List<String> spotsList = scenicGroupSpotVo.getSpotList();
			if (spotsList!=null && spotsList.size()>0) {
				List<ScenicSpotGroup> spotGroups = new ArrayList<>();
				spotsList.forEach(spot -> {
					ScenicSpotGroup scenicSpotGroup = new ScenicSpotGroup();
					scenicSpotGroup.setId(UUIDUtil.randomUUID32());
					scenicSpotGroup.setCreateTime(new Date());
					scenicSpotGroup.setUpdateTime(new Date());
					scenicSpotGroup.setGroupId(scenicGroupSpotVo.getScenicGroupId());
					scenicSpotGroup.setScenicSpotId(spot);
					spotGroups.add(scenicSpotGroup);
				});
				scenicGroupAndSpotMapper.insertScenicSpotGroupBatch(spotGroups);
				return JsonResult.ok();
			} else {
				return JsonResult.error("提交景点数据为空，添加景点失败");
			}
		} else {
			return JsonResult.error("提交数据为空，添加景点失败");
		}
	}

	@Override
	@Transactional
	public JsonResult delSpot(ScenicGroupSpotVo scenicGroupSpotVo) {
		if (scenicGroupSpotVo!=null && StringUtils.isNotBlank(scenicGroupSpotVo.getScenicGroupId()) && scenicGroupSpotVo.getSpotList()!=null && scenicGroupSpotVo.getSpotList().size()>0) {
			scenicGroupAndSpotMapper.deleteScenicSpotGroupBatch(scenicGroupSpotVo);
			return JsonResult.ok();
		} else {
			return JsonResult.error("提交数据为空，删除景点失败");
		}
	}

	@Override
	public JsonResult spotSort(List<ScenicSpotGroup> ScenicSpotGroups) {
		
		return null;
	}

}
